﻿// ****************************************************************************
// <copyright file="MultiTouchBehavior.Silverlight4.cs" company="Davide Zordan">
// Copyright © Davide Zordan 2010-2011
// </copyright>
// ****************************************************************************
// <author>Davide Zordan</author>
// <email>info@davidezordan.net</email>
// <date>27.08.2010</date>
// <project>MultiTouch.Behaviors.Silverlight4</project>
// <web>http://multitouch.codeplex.com/</web>
// <license>
// See http://multitouch.codeplex.com/license.
// </license>
// <LastBaseLevel>BL0001</LastBaseLevel>
// ****************************************************************************

using System;
using System.Windows;
using System.Windows.Input;

namespace MultiTouch.Behaviors.Silverlight4
{
        /// <summary>
        /// Implements Multi-Touch Manipulation
        /// </summary>
        public partial class MultiTouchBehavior
        {
            private MultiTouchManipulationBehavior _multiTouchManipulationBehavior;

            private void OnAttachedImpl()
            {
                _multiTouchManipulationBehavior=new MultiTouchManipulationBehavior();
                _multiTouchManipulationBehavior.Attach(AssociatedObject);
                _multiTouchManipulationBehavior.IsPivotEnabled = IsPivotEnabled;
                _multiTouchManipulationBehavior.IsInertiaEnabled = IsInertiaEnabled;
                _multiTouchManipulationBehavior.AreFingersVisible = AreFingersVisible;
                _multiTouchManipulationBehavior.IsRotateEnabled = IsRotateEnabled;
                _multiTouchManipulationBehavior.IsScaleEnabled = IsScaleEnabled;
                _multiTouchManipulationBehavior.IsTranslateEnabled = IsTranslateXEnabled && IsTranslateYEnabled;
                _multiTouchManipulationBehavior.MaximumScaleRadius = MaximumScale*3.6;
                _multiTouchManipulationBehavior.MinimumScaleRadius = MinimumScale*3.6;
            }

            private void OnProcessorDelta(object sender, EventArgs e)
            {

            }

            private void OnDetachingImpl()
            {
                if (_multiTouchManipulationBehavior!=null)
                {
                    _multiTouchManipulationBehavior.Detach();
                }
            }

            private static void OnIsInertiaEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                if ((sender is MultiTouchBehavior) && (e.NewValue != null) && (((MultiTouchBehavior) sender)._multiTouchManipulationBehavior!=null))
                {
                    ((MultiTouchBehavior) sender)._multiTouchManipulationBehavior.IsInertiaEnabled = (bool) e.NewValue;
                }
            }

            private static void OnIsScaleEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                if ((sender is MultiTouchBehavior) && (e.NewValue != null) && (((MultiTouchBehavior)sender)._multiTouchManipulationBehavior != null))
                {
                    ((MultiTouchBehavior)sender)._multiTouchManipulationBehavior.IsScaleEnabled = (bool)e.NewValue;
                }
            }

            private static void OnIsRotateEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                if ((sender is MultiTouchBehavior) && (e.NewValue != null) && (((MultiTouchBehavior)sender)._multiTouchManipulationBehavior != null))
                {
                    ((MultiTouchBehavior)sender)._multiTouchManipulationBehavior.IsRotateEnabled = (bool)e.NewValue;
                }
            }

            private static void OnIsTranslateXEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                if ((sender is MultiTouchBehavior) && (e.NewValue != null) && (((MultiTouchBehavior)sender)._multiTouchManipulationBehavior != null))
                {
                    ((MultiTouchBehavior)sender)._multiTouchManipulationBehavior.IsTranslateEnabled = (bool)e.NewValue;
                }
            }

            private static void OnIsTranslateYEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                if ((sender is MultiTouchBehavior) && (e.NewValue != null) && (((MultiTouchBehavior)sender)._multiTouchManipulationBehavior != null))
                {
                    ((MultiTouchBehavior)sender)._multiTouchManipulationBehavior.IsTranslateEnabled = (bool)e.NewValue;
                }
            }

            private static void OnMinimumScaleChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                if ((sender is MultiTouchBehavior) && (e.NewValue != null) && (((MultiTouchBehavior)sender)._multiTouchManipulationBehavior != null))
                {
                    ((MultiTouchBehavior)sender)._multiTouchManipulationBehavior.MinimumScaleRadius = (int)e.NewValue*3.6;
                }
            }

            private static void OnMaximumScaleChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                if ((sender is MultiTouchBehavior) && (e.NewValue != null) && (((MultiTouchBehavior)sender)._multiTouchManipulationBehavior != null))
                {
                    ((MultiTouchBehavior)sender)._multiTouchManipulationBehavior.MaximumScaleRadius = (int)e.NewValue * 3.6;
                }
            }

            private static void OnIsPivotEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                if ((sender is MultiTouchBehavior) && (e.NewValue != null) && (((MultiTouchBehavior)sender)._multiTouchManipulationBehavior != null))
                {
                    ((MultiTouchBehavior)sender)._multiTouchManipulationBehavior.IsPivotEnabled = (bool)e.NewValue;
                }
            }

            private bool _isDebugModeActive;
            public bool IsDebugModeActive
            {
                get
                {
                    return _isDebugModeActive;
                }
                set
                {
#if DEBUG
                    _isDebugModeActive = value;
#endif
                }
            }

            private bool _areFingersVisible;
            public bool AreFingersVisible
            {
                get
                {
                    return _areFingersVisible;
                }
                set
                {
#if DEBUG
                    _areFingersVisible = value;
#endif
                }
            }

            void TouchFrameReported(object sender, TouchFrameEventArgs e)
            {

            }


            /// <summary>
            /// Performs the actual move.
            /// </summary>
            public void Move(Point center, double orientation, double radius)
            {
                if (_multiTouchManipulationBehavior != null)
                {
                    _multiTouchManipulationBehavior.Move(center, orientation, radius);
                }
            }
        }
}
